summaryrefslogtreecommitdiffstats
path: root/heimdall-frontend/source/qml/DropFiles.qml
blob: bc96cbcd2478ec58f76af4d6a39ab20159ba1c89 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Layouts 1.1
import QtQuick.Dialogs 1.2
import "ArrayExtensions.js" as ArrayExtensions
import "FileUtils.js" as FileUtils
import HeimdallFrontend 1.0 as Native

DropFilesForm {
	id: background
	property var fileUrls: []
	signal nextPressed(var files)

	ListModel {
		id: fileModel
	}

	Native.PackageData {
		id: packageData
	}

	function setFileGridVisible(visible) {
		if (fileGridContainer.visible !== visible) {
			dropFilesContainer.visible = !visible;
			bottomButtonsContainer.visible = visible;
			fileGridContainer.visible = visible;

			if (visible) {
				fileGridView.forceActiveFocus();
			}
		}
	}

	function addFiles(urls) {
		var count = urls.length;
		if (count > 0) {
			for (var i = 0; i < count; i++) {
				if (FileUtils.isFile(urls[i])) {
					if (FileUtils.isArchive(urls[i])) {
						var packageData = Native.Firmware.extractArchive(urls[i]);
						packageData.filePaths.forEach(function(path) {
							var filename = FileUtils.filenameFromPath(path);
							fileModel.append({ icon: "drop_zone.svg", text: filename });
						});
						fileUrls.extend(packageData.filePaths);
					} else {
						var filename = FileUtils.filenameFromUrl(urls[i]);
						fileModel.append({ icon: "drop_zone.svg", text: filename });
						fileUrls.push(urls[i]);
					}
				}
			}

			setFileGridVisible(true);
		}
	}

	function removeFile(index) {
		if (index >= 0) {
			fileModel.remove(index);
			fileUrls.splice(index, 1);

			if (fileUrls.length == 0) {
				setFileGridVisible(false);
			}
		}
	}

	onKeyPressed: {
		if (event.key === Qt.Key_Delete || event.key === Qt.Key_Backspace) {
			event.accepted = true;
			removeFile(fileGridView.currentIndex);
		}
	}

	dropFilesArea {
		onDropped: {
			if ((drop.action == Qt.CopyAction || drop.action == Qt.MoveAction) && drop.urls.length > 0) {
				addFiles(drop.urls);
				drop.acceptProposedAction();
				drop.accept(Qt.CopyAction);
			}
		}
	}

	browseButton.onClicked: {
		browseDialog.open()
	}

	bottomBrowseButton.onClicked: {
		browseDialog.open()
	}

	nextButton {
		onClicked: nextPressed(fileUrls)
	}

	fileGridView {
		model: fileModel
	}

	FileDialog {
		id: browseDialog
		title: "Select firmware file(s)"
		selectMultiple: true
		selectFolder: false
		onAccepted: {
			addFiles(browseDialog.fileUrls);
		}
	}
}